import {Utils} from '../common.js';
//import * as WidgetEvent from './widget-event.js'
//import {ToastMessager, MessagerLevel} from '../messager.js'
import D from "../delegator.js";
import {WIDGET_DISPLAY_MODE, WIDGET_TYPE} from './widget-const.js';
import PaneFactory from './pane-factory.js';
import * as Pane from "./pane.js"
import * as UI from "../ui.js"

export class Widget {
    constructor(widgetContent, mode, updateFn) {
        Utils.DefineProperty(this, "type", widgetContent.type, false, false, false);
        this._viewPane = undefined;
        this._editPane = undefined;
        this._updateFn = updateFn;
        this._dirty = false;
        this._mode = WIDGET_DISPLAY_MODE.UNKNOWN;
        this._widgetContent = widgetContent;
        this._ui = UI.UIWidget(widgetContent.SUID);
        this._SetModeTo(mode ?? WIDGET_DISPLAY_MODE.VIEW);
    }
    get element() {return this._ui.element;}
    get WID() {return this._widgetContent.SUID;}
    get mode() {return this._mode;}
    get ui() {return this._ui;}
    get dirty(){return this._dirty;}

    Destroy() {
        this._viewPane && this._viewPane.Destroy();
        this._editPane && this._editPane.Destroy();
        this._ui.Release();
        this._ui = undefined;
        this._updateFn = undefined;
        return this;
    }
    SetDirty() {this._dirty = true;}
    SetClean() {this._dirty = false;}
    Aim() {
        location.href = location.origin + `/#${this.WID}`;
    }
    Focus() {
        this._editPane && this._editPane.SetFocus();
    }
    Blur() {
        this._editPane && this._editPane.SetBlur();
    }
    ShowEditPane() {
        return this._SetModeTo(WIDGET_DISPLAY_MODE.EDIT);
    }
    ShowViewPane() {
        this._editPane && this._editPane.SetBlur();
        return this._SetModeTo(WIDGET_DISPLAY_MODE.VIEW);
    }
    Render() {
        this._viewPane && this._viewPane.Render();
    }
    Upload() {
        if(this._editPane) {
            this._editPane.Upload();
        }
    }
    _CreatePane() {
        switch(this._mode) {
            case WIDGET_DISPLAY_MODE.EDIT:
                this._editPane = PaneFactory.CreateEditPane(this.type, this._widgetContent);
                this._editPane.SetUpdateCallback(this._updateFn);
                break;
            case WIDGET_DISPLAY_MODE.VIEW:
            default:
                this._viewPane = PaneFactory.CreateViewPane(this.type, this._widgetContent);
                this._viewPane.Render();
        }
    }

    _SetModeTo(mode) {
        if(mode === this._mode) return this;
        this._mode = mode;
        switch(mode) {
            case WIDGET_DISPLAY_MODE.VIEW:
                if(!this._viewPane) {
                    this._CreatePane();
                }
                this._ui.SetPane(this._viewPane.ui);
                break;
            case WIDGET_DISPLAY_MODE.EDIT:
                if(!this._editPane) {
                    this._CreatePane();
                }
                this._ui.SetPane(this._editPane.ui);
                break;
            default:
            //ToastMessager.Show(`Can NOT set widget mode to ${mode}`, MessagerLevel.WARN);
        }
        return this;
    }
}

export class PlaceholderWidget {
    constructor() {
        Utils.DefineProperty(this, "type", WIDGET_TYPE.PLACE_HOLDER, false, false, false);
        this._ui = D(Layout.layout).Qe('#slog_placeholder_widget');
    }
    get element() {return this._ui;}
    get SUID() {return "__placeholder_widget__";}
    Destroy() {}
}
PaneFactory.RegistPane(WIDGET_TYPE.TEXT, Pane.TextViewPane, Pane.TextEditPane);
PaneFactory.RegistPane(WIDGET_TYPE.HTML, Pane.HTMLViewPane, Pane.HTMLEditPane);
PaneFactory.RegistPane(WIDGET_TYPE.MARKDOWN, Pane.MarkdownViewPane, Pane.MarkdownEditPane);
PaneFactory.RegistPane(WIDGET_TYPE.CODE, Pane.CodeViewPane, Pane.CodeEditPane);

